package cn.ctyun.ipush.service.impl;

import cn.ctyun.ipush.dao.TaskDetailDao;
import cn.ctyun.ipush.model.DataSourceModel;
import cn.ctyun.ipush.model.TaskDetailModel;
import cn.ctyun.ipush.service.TaskDetailService;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.TaskDetailVo;
import com.dexcoder.commons.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-6-19
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
@Service("taskDetailService")
public class taskDetailServiceImpl implements TaskDetailService {

    @Autowired
    private TaskDetailDao taskDetailDao;

    public void save(TaskDetailVo taskDetailVo) {
        taskDetailVo.setTaskDetailModelId(UUIDUtils.getUUID32());
        taskDetailVo.setCreateDate(DateUtils.dateToString(new Date()));
        TaskDetailModel taskDetailModel = taskDetailVo.getTargetObject(TaskDetailModel.class);
        //vo中的sqllist进行转义
        StringBuffer stringBuffer = new StringBuffer();
        for (String cronStr : taskDetailVo.getCronListVo()) {
            stringBuffer.append(cronStr).append("$");
        }
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        taskDetailModel.setCronExpression(stringBuffer.toString());
        //保存到数据库中
        save(taskDetailModel);
    }

    public void save(TaskDetailModel taskDetailModel) {
        taskDetailDao.save(taskDetailModel);
    }

    public int delete(String taskDetailId) {
        return taskDetailDao.delete(taskDetailId);
    }

    public int update(TaskDetailVo taskDetailVo) {
        if (StringUtils.isEmpty(taskDetailVo.getTaskDetailModelId())) {
            throw new RuntimeException("taskDetail 主键ID 不存在");
        }
        //todo 额外处理
        TaskDetailModel taskDetailModel = taskDetailVo.getTargetObject(TaskDetailModel.class);
        TaskDetailModel taskDetailModelDb = querySingleResult(taskDetailModel.getTaskDetailModelId());

        if (taskDetailModel.getTaskName() != null)
            taskDetailModelDb.setTaskName(taskDetailModel.getTaskName());
        if (taskDetailVo.getCronListVo() != null) {
            //vo中的sqllist进行转义
            StringBuffer stringBuffer = new StringBuffer();
            for (String cronStr : taskDetailVo.getCronListVo()) {
                stringBuffer.append(cronStr).append("$");
            }
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
            taskDetailModelDb.setCronExpression(stringBuffer.toString());
        }
        return taskDetailDao.update(taskDetailModelDb);
    }

    @Override
    public int update(TaskDetailModel taskDetailModel) {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }


    public TaskDetailVo querySingleResultVo(String taskDetailId) {
        TaskDetailModel taskDetailModel = querySingleResult(taskDetailId);
        return taskDetailModel.getTargetObject(TaskDetailVo.class);
    }

    public TaskDetailModel querySingleResult(String taskDetailId) {
        return taskDetailDao.querySingleResult(taskDetailId);
    }

    public List<TaskDetailModel> queryList(TaskDetailModel taskDetailModel) {
        return null;
    }

    public List<TaskDetailVo> queryListVo(TaskDetailVo taskDetailVo) {
        return null;
    }

    public boolean isExist(TaskDetailModel taskDetailModel) {
        return taskDetailDao.isExist(taskDetailModel);
    }
}
